package com.lzc.tlmail.tlmailware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.lzc.common.to.SkuWareVo;
import com.lzc.tlmail.tlmailware.Vo.LockWareOrdervo;
import com.lzc.common.exception.NoSuchWareException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lzc.tlmail.tlmailware.entity.WareSkuEntity;
import com.lzc.tlmail.tlmailware.service.WareSkuService;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.R;



/**
 * 商品库存
 *
 * @author lzc
 * @email 1029669636@gmail.com
 * @date 2020-07-26 19:19:12
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 锁定订单的要锁的库存 验证能不能提交订单 判断有没有库存
     * @return
     */
    @PostMapping("/lockWareOrder")
    public R lockWareOrder(@RequestBody LockWareOrdervo lockWareOrdervo){
        try {
            Boolean result=wareSkuService.lockWareOrder(lockWareOrdervo);
            System.out.println(result.toString());
            return R.ok();
        }catch (NoSuchWareException e){
            return R.error(500,"库存不足");
        }

    }


    //查询sku是否有库存
    @PostMapping("/hasstock")
    public List<SkuWareVo> getSkusHasStock(@RequestBody List<Long> skuIds){
      List<SkuWareVo> skuWareVos=wareSkuService.getSkusHasStock(skuIds);
      return skuWareVos;
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("tlmailware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("tlmailware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("tlmailware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("tlmailware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("tlmailware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
